<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">问卷记录</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>问卷记录</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="paper-record-edit-search" class="layui-input search-input" type="number"
                      placeholder="输入问卷id"/>&emsp;
            <button id="paper-record-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
            </button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="paper-record-table" lay-filter="paper-record-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="paper-record-state">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="show">查看</a>
</script>
<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let admin = layui.admin;

        let loginUser = config.getUser();

        let url = config.base_server + 'api-paper/paperRecord/getPaperRecords'+'?access_token='+config.getToken().access_token;
        let params = {
            userid: loginUser.id,
            // access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'title', sort: true, title: '问卷标题'},
            {field: 'rateCodeStr', sort: true, title: '评分类型'},
            {field: 'writeUser', sort: true, title: '填写用户'},
            {field: 'score', sort: true, title: '评分分数'},
            {field: 'pointsRate', sort: true, title: '总体评价'},
            {field: 'finishTime', sort: true, title: '完成时间'},
            {templet: '#paper-record-state', width: 140, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#paper-record-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(paper-record-table)', function (obj) {
            let layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-paper/paperRecord/delete?id=' + obj.data.id + '&userId=' + loginUser.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('paper-record-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            } else if (layEvent === 'show') {
                showPaper(obj.data.paperId);
            }
        });

        // 搜索按钮点击事件
        $('#paper-record-btn-search').click(function () {
            let key = $('#paper-record-edit-search').val();
            table.reload('paper-record-table', {where: {searchKey: key}});
        });

        // 问卷记录
        let showPaper = function (paperId) {
            let data = {};
            admin.req('api-paper/paper/findById/' + paperId, {}, function (result) {
                layer.closeAll('loading');
                data = result.data;
            }, 'GET');

            admin.putTempData('t_showPaper', data);
            let title = '问卷记录';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/paperRecord_form.html',
                area: '1200px',
                offset: '50px',
                finish: function () {
                    table.reload('paper-record-table', {});
                }
            });
        };
    });
</script>